﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using AspNetCs.Model;
using System.Data;
using AspNetCs.Config;
using AspNetCs.Common;
namespace AspNetCs.SQLServerDAL
{
    public class Announce_DAL : IAnnounce
    {

        #region IAnnounce 成员

        public int Add(Announce_Model model)
        {
            db ConnAdd = new db();
            int result = -1;
            try
            {
                db.ParameterArry[] Arr = new db.ParameterArry[5];
                Arr[0].dbType = SqlDbType.VarChar;
                Arr[0].objValue = model.Title;
                Arr[0].parName = "@Title";


                Arr[1].dbType = SqlDbType.Text;
                Arr[1].objValue = model.Content;
                Arr[1].parName = "@Content";


                Arr[2].dbType = SqlDbType.DateTime;
                Arr[2].objValue = model.Addtime;
                Arr[2].parName = "@Addtime";

                Arr[3].dbType = SqlDbType.Int;
                Arr[3].objValue = model.Hot;
                Arr[3].parName = "@Hot";

                Arr[4].dbType = SqlDbType.Int;
                Arr[4].objValue = model.IsNew;
                Arr[4].parName = "@isNew";

                result = ConnAdd.ExcSql("Insert into Announce(title,Content,addtime,hot,isNew) values(@Title,@Content,@Addtime,@Hot,@isNew)", Arr);
            }
            catch (Exception Ex)
            {

                new CustomException(Ex);
            }
            finally
            {
                ConnAdd.close();
            }
            return result;
        }

        public int del(int id)
        {
            db ConnDel = new db();
            int result = -1;
            try
            {
                db.ParameterArry[] Arr = new db.ParameterArry[1];
                Arr[0].dbType = SqlDbType.Int;
                Arr[0].objValue = id;
                Arr[0].parName = "@id";
                result = ConnDel.ExcSql("delete From Announce where id=@id", Arr);
            }
            catch (Exception Ex)
            {
                new CustomException(Ex);
            }
            finally
            {
                ConnDel.close();
            }
            return result;

        }

        //修改
        public int update(Announce_Model model)
        {
            db ConnUp = new db();
            int result = -1;
            try
            {
                db.ParameterArry[] Arr = new db.ParameterArry[6];

                Arr[5].dbType = SqlDbType.Int;
                Arr[5].objValue = model.Id;
                Arr[5].parName = "@id";


                Arr[0].dbType = SqlDbType.VarChar;
                Arr[0].objValue = model.Title;
                Arr[0].parName = "@Title";


                Arr[1].dbType = SqlDbType.Text;
                Arr[1].objValue = model.Content;
                Arr[1].parName = "@Content";


                Arr[2].dbType = SqlDbType.DateTime;
                Arr[2].objValue = model.Addtime;
                Arr[2].parName = "@Addtime";

                Arr[3].dbType = SqlDbType.Int;
                Arr[3].objValue = model.Hot;
                Arr[3].parName = "@Hot";

                Arr[4].dbType = SqlDbType.Int;
                Arr[4].objValue = model.IsNew;
                Arr[4].parName = "@isNew";

                result = ConnUp.ExcSql("Update Announce Set Title=@Title,Content=@Content,Addtime=@Addtime,Hot=@Hot,isNew=@isNew where id=@id", Arr);
            }
            catch (Exception Ex)
            {

                new CustomException(Ex);
            }
            finally
            {
                ConnUp.close();
            }
            return result;
        }

        public Announce_Model GetModel(System.Data.DataRow dr)
        {
            Announce_Model model = new Announce_Model();
            if (dr["id"] != null) { model.Id = StringHelper.isNumber_int(dr["id"].ToString()); }
            if (dr["Title"] != null) { model.Title = dr["Title"].ToString(); }
            if (dr["Content"] != null) { model.Content = dr["Content"].ToString(); }
            if (dr["Addtime"] != null) { model.Addtime = Convert.ToDateTime(dr["Addtime"].ToString()); }
            if (dr["hot"] != null) { model.Hot = StringHelper.isNumber_int(dr["hot"].ToString()); }
            if (dr["isNew"] != null) { model.IsNew = StringHelper.isNumber_int(dr["isNew"].ToString()); }
            return model;
        }

        public Announce_Model GetModel(int id)
        {
            Announce_Model model = new Announce_Model();
            db ConnGet = new db();
            try
            {
                db.ParameterArry[] Arr = new db.ParameterArry[1];
                Arr[0].dbType = SqlDbType.Int;
                Arr[0].objValue = id;
                Arr[0].parName = "@id";
                DataTable dt = ConnGet.GetSql_DataTable("Select Top 1 *  From Announce where id=@id", Arr);
                foreach (DataRow dr in dt.Rows)
                {
                    model = GetModel(dr);
                }
            }
            catch (Exception Ex)
            {
                new CustomException(Ex);
            }
            finally
            {
                ConnGet.close();
            }
            return model;
        }

        public DataTable GetAnnounceList(string filter)
        {
            DataTable dataTable = new DataTable();
            db db = new db();
            if (filter != "")
            {
                filter = " where " + filter;
            }
            string cmdText = "select * from Announce  " + filter;
            dataTable = db.GetSql_DataTable(cmdText);
            return dataTable;
        }




        #endregion
    }
}
